package com.bikes.mileagetracker.e2e;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.bikes.mileagetracker.MainActivity;
import com.bikes.mileagetracker.R;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.isDialog;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.anything;

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> loginRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void e2eMainActivityTest(){
        Espresso.onView(withId(R.id.vw_user_spinner))  // find the view
                .check(matches(isDisplayed())) // check view is display on the current activity.
                .perform(ViewActions.click());
        Espresso.onData(anything()).atPosition(1).perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_rm_button))  // find the view
                .check(matches(isDisplayed())) // check view is display on the current activity.
                .perform(ViewActions.click());

        Espresso.onView(withText("OK")).inRoot(isDialog())
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_edit_button))
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_change_name))
                .check(matches(isDisplayed()))
                .perform(ViewActions.typeText("helloStudent")).perform(closeSoftKeyboard());

        Espresso.onView(withText("OK")).inRoot(isDialog())
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_add_button))
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_input_name))
                .check(matches(isDisplayed()))
                .perform(ViewActions.typeText("hello")).perform(closeSoftKeyboard());

        Espresso.onView(withText("OK")).inRoot(isDialog())
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_start_stop_button))
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.onView(withId(R.id.vw_googlpmap_icon))
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.pressBack();

        Espresso.onView(withId(R.id.vw_view_log_button))
                .check(matches(isDisplayed()))
                .perform(ViewActions.click());

        Espresso.pressBack();
    }
}
